package com.aglhz.nature.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.aglhz.nature.R;
import com.aglhz.nature.adapter.AroundGridViewAdapter.ViewHolder;
import com.aglhz.nature.modle.item.classification.ListData;
import com.aglhz.nature.view.SecondHomeWebActivity;
import com.squareup.picasso.Picasso;

public class RecommendGridAdapter extends BaseAdapter{
	public List<ListData>mData;
    public Context context;
	
    public RecommendGridAdapter(List<ListData> mData, Context context) {
		super();
		this.mData = mData;
		this.context = context;
	}
    
    @Override
	public int getCount() {
		return mData.size();
	}
	@Override
	public Object getItem(int position) {
		return  mData.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		 ViewHolder viewHolder; 
			if(null==convertView){			
				  convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend_gridview, null); 
				  viewHolder = new ViewHolder();
				  viewHolder.recommend_gridview_imageview = (ImageView) convertView.findViewById(R.id.recommend_gridview_imageview); 
				  viewHolder.textName=(TextView) convertView.findViewById(R.id.textName);
				  viewHolder.recommend_gridview_imageview.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
					Intent intent=new Intent(context,SecondHomeWebActivity.class);
					intent.putExtra("homeRE3",mData.get(position).getLink());
					context.startActivity(intent);
						
					}
				});
				  
				  convertView.setTag(viewHolder); 
			}else{
				viewHolder = (ViewHolder) convertView.getTag(); 
			}
			
			
			//设置图片
			
				  String imgUrl = mData.get(position).getGoodsImgCover();
				//picasso加载网络图片
				   Picasso.with(context)  
				   .load(imgUrl)  	
				    .resize(getScreenWidth()/2, getScreenWidth()/2)
				   .into(viewHolder.recommend_gridview_imageview) ;
				  
				   viewHolder.textName.setText( mData.get(position).getGoodsName());
					
//			int screenWidth = getScreenWidth();
//			
//			GridView.LayoutParams p=new GridView.LayoutParams(screenWidth/2,screenWidth/2);
//			convertView.setLayoutParams(p);

			return convertView;
		}
		class ViewHolder 
		{  
		    public ImageView recommend_gridview_imageview; 
		    public TextView textName;
		} 
		
		/**
		 * 获取屏幕宽度
		 * 
		 * @return
		 */
		protected int getScreenWidth() {
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Display display = wm.getDefaultDisplay();
			return display.getWidth();
		}
	
    
}
